// import * as echarts from '../ec-canvas/echarts.js';
var actbusiness = require('../model/business_work')
var wxCharts = require('./wxcharts.js');
var lineChart = null;
var util=require('../../../utils/util.js')
var nowdate=util.formatDate(new Date());
var nowmonth=util.formatMonth(new Date());
var nowyear=util.formatYear(new Date());


const app = getApp();

Page({
  mixins: [require('../../../xc/mixins/basicMixin')],

  /**
   * 页面的初始数据
   */
  data: {
    
    condition: {
      way: "money",
      time: "date",
      date:nowdate,
      month:nowmonth,
      year:nowyear

    },
    // show:false,
    conditionnav: [{
        name: "统计指标",
        val: "way",
        nav: [{
          val: "money",
          name: "金额"
        }, {
          val: "order",
          name: "订单"
        }, {
          val: "member",
          name: "访客"
        }]
      },
      {
        name: "时间跨度",
        val: "time",
        nav: [{
          val: "date",
          name: "日"

        }, {
          val: "month",
          name: "月"
        }, {
          val: "year",
          name: "年"
        }]
      },
    ],
    active: 0
  },
  touchHandler: function (e) {

    lineChart.showToolTip(e, {
      format: function (item, category) {
        return category + ' ' + item.name + ':' + item.data
      }
    });
  },
  bindDateChange(e){
    var key=e.currentTarget.dataset.key;
    var name='condition.'+key
    this.setData({
      [name]: e.detail.value
    })
    this.getline()
  },
 
  onChange(e) {
    this.setData({
      active: e.detail.name
    })
    if(e.detail.name==1){
      this.getline()
    }
  },
  getStatistics(id) {
    var that = this;
    actbusiness.statistics(id).then(res => {

      if (res["status"] == 1) {
        if (res["obj"]['payChange']) {
          res["obj"]['payChange'] = res["obj"]['payChange'].toFixed(2)
        }
        that.setData({
          statistics: res["obj"]
        })
        
      }
    })
  },
  afterlogin: function (options, config, userinfo) {
    var that = this;
    if (userinfo.memberinfo) {
      if (userinfo.memberinfo.authority == 2) {
        that.getStatistics(0)
      } else {
        that.nopow(config);
      }
    } else {
      var app = getApp()
      app.xc_sys_config.authop.oppower("login").then(function (res) {
        if (res["obj"].memberinfo.authority == "2") {
          that.getStatistics(0)

        } else {
          that.nopow(config);

        }
        return
      })
    }
  },
  nopow(config) {
    wx.showModal({
      title: "你没有访问权限",
      content: "你没有访问权限",
      showCancel: true,
      success() {
    

        wx.redirectTo({
          url: config.basic.homeurl,
        })
      }

    })
  },
  xcloaddata: function (options, config) {

  },
  changecondition(e) {
 
    var dataset = e.currentTarget.dataset;
    var name = "condition." + dataset.key;
    this.setData({
      [name]: dataset.val
    })
    this.getline()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getline()
    
  },
  line(){
    var windowWidth = 320;
    try {
        var res = wx.getSystemInfoSync();
        windowWidth = res.windowWidth - 15;
    } catch (e) {
        console.error('getSystemInfoSync failed!');
    }
    
    var simulationData = this.data.linedata;
    // console.log("simulationData")
    // console.log(simulationData)
    lineChart = new wxCharts({
      canvasId: 'lineCanvas',
      type: 'line',
      color: ['#333333'],
      categories: simulationData.categories,
      animation: true,
      // background: '#f5f5f5',
      series: [{
          name:simulationData.name,
          data: simulationData.data,
          color:this.data.themcolor.primarycolor,
          format: function (val, name) {
            return val+ simulationData.unit;
          }
        },
        // {
        //     name: '成交量2',
        //     data: [2, 0, 0, 3, null, 4, 0, 0, 2, 0],
        //     format: function (val, name) {
        //         return val.toFixed(2) + '万';
        //     }
        // }
      ],
      xAxis: {
        disableGrid: true
      },
      yAxis: {
        title: simulationData.title,
        format: function (val) {
          return val.toFixed(2);
        },
        min: 0
      },
      width: windowWidth,
      height: 200,
      dataLabel: false,
      dataPointShape: true,
      extra: {
        lineStyle: 'curve'
      }
    });
  },
  //获取统计图的数据
  getline(){
    var that = this;
    actbusiness.getline(this.data.condition).then(res => {
      if (res["status"] == 1) {
        that.setData({
          linedata: res["obj"]
        })
        console.log("linedata")
        console.log(res["obj"])
        that.line()
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  
})